home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / games / IndiZone / sw / comm.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  7.2 KB  |  278 lines

  1. /*
  2.  * Copyright (C) 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. #ifndef COMM_H
  18. #define    COMM_H
  19.  
  20. #include <sys/types.h>
  21. #include <netinet/in.h>
  22. #include "sw.h"
  23.  
  24. typedef struct in_addr    InAddr;
  25. typedef fd_set        FD_TYPE;
  26.  
  27. struct NetId {
  28.   public:
  29.             NetId();
  30.             NetId(const char* addr_in_network_byte_order);
  31.             NetId(InAddr& a);
  32.             NetId(InAddr* a);
  33.             operator InAddr () const;
  34.     int            operator == (const NetId&) const;
  35.     int            operator != (const NetId&) const;
  36.     int            isAny() const;
  37.  
  38.   public:
  39.     InAddr        id;            // network id
  40. };
  41.  
  42. struct PlayerInfo {
  43.   public:
  44.     InAddr        id;
  45.     Team        team;
  46.     char        name[MAXNAMELEN];
  47.     ShipClass        shipClass;
  48. };
  49.  
  50. struct TeamInfo {
  51.   public:
  52.     Team        team;            // team this is info for
  53.     int            players;        // players on this team
  54.     int            won, lost;        // team score
  55.     float        position[3];        // flag position
  56.     FlagState        state;            // flag status
  57.     InAddr        owner;            // ship that has flag (if any)
  58. };
  59.  
  60. // Information that all objects in space need to have
  61. // (No pointers or virtual functions allowed, nor objects with the same!)
  62. // If active is not ObjectActive, no other fields have any valid meaning
  63. struct ObjectInfo {
  64.   public:
  65.     Active        active;            // ObjectActive if really exists
  66.     float        mass;            // object mass
  67.     float        position[3];        // xyz
  68.     float        velocity[3];        // dx dy dz
  69.     float        orientation[4];        // quaternion orientation
  70.     float        direction[3];        // thrust direction
  71.     float        explodeTime;        // exploding if > 0.0
  72.     float        engineOutput;        // fraction of max (0-1)
  73. };
  74.  
  75. // All the data in ShipInfo is broadcast to other systems
  76. // (No pointers or virtual functions allowed, nor objects with the same!)
  77. struct ShipInfo {
  78.   public:
  79.     InAddr        myId;            // my host address
  80.  
  81.     // game info
  82.     int            won, lost;        // score
  83.     Team        flag;            // which flag ship has
  84.  
  85.     // spacecraft info
  86.     ObjectInfo        info;
  87.  
  88.     // laser info (position & direction only set if firingLaser is TRUE)
  89.     int            firingLaser;        // boolean
  90.     float        laserDirection[3];    // direction laser points in
  91.     float        beamLength;        // distance to hit
  92.  
  93.     // missile info
  94.     ObjectInfo        missile[MAXMISSILES];    // missile info
  95.  
  96.     // shield info
  97.     float        shieldStrength[6];    // fraction max strength
  98.     float        shieldHits[MAXHITS][4];    // frac max (1), position (3)
  99. };
  100.  
  101. struct BroadcastPacket {
  102.   public:
  103.     InAddr        serverId;        // host address of server
  104.     int            length;            // message length
  105.     ShipInfo        message;
  106. };
  107.  
  108. // List of all message types to and from server
  109. enum ServerMessageType    { SwNone,        // no message
  110.               SwJoin,        // (to) join game
  111.               SwQuit,        // (to) leave game
  112.               SwGetWorld,        // (to) request world database
  113.               SwAlive,        // (to) now alive
  114.               SwGrabFlag,        // (to) request for flag
  115.               SwDropFlag,        // (to) release flag
  116.               SwCaptureFlag,    // (to) captured flag
  117.               SwAddPlayer,        // (from) new player joined
  118.               SwRemovePlayer,    // (from) player quit
  119.               SwFlagUpdate,        // (from) team and flag info
  120.               SwFlagGrabbed,    // (from) flag grabbed
  121.               SwFlagDropped,    // (from) flag released
  122.               SwFlagCaptured,    // (from) flag captured
  123.               SwWorld,        // (from) world info
  124.               SwKilled,        // (to/from) someone killed
  125.               SwHit,        // (to/from) hit something
  126.               SwMessage        // (to/from) info message
  127.             };
  128.  
  129. // Each message has it's own information structure
  130. struct SwJoinMessage {
  131.   public:
  132.     PlayerInfo        p;            // info on player
  133. };
  134.  
  135. struct SwQuitMessage {
  136.   public:
  137.     // no data
  138. };
  139.  
  140. struct SwGetWorldMessage {
  141.   public:
  142.     // no data
  143. };
  144.  
  145. struct SwAliveMessage {
  146.   public:
  147.     // no data
  148. };
  149.  
  150. struct SwGrabFlagMessage {
  151.   public:
  152.     Team        team;            // team number whose flag it is
  153. };
  154.  
  155. struct SwDropFlagMessage {
  156.   public:
  157.     Team        team;            // team whose flag it is
  158.     float        position[3];        // where dropped
  159. };
  160.  
  161. struct SwCaptureFlagMessage {
  162.   public:
  163.     Team        flagTeam;        // team whose flag it is
  164.     Team        captorTeam;        // team who captured flag
  165. };
  166.  
  167. struct SwAddPlayerMessage {
  168.   public:
  169.     PlayerInfo        p;            // info on player
  170. };
  171.  
  172. struct SwRemovePlayerMessage {
  173.   public:
  174.     InAddr        id;            // id of player quiting
  175. };
  176.  
  177. struct SwFlagUpdateMessage {
  178.   public:
  179.     TeamInfo        info;            // team information
  180. };
  181.  
  182. struct SwFlagGrabbedMessage {
  183.   public:
  184.     InAddr        grabber;        // ship that grabbed flag
  185.     TeamInfo        info;            // new team information
  186. };
  187.  
  188. struct SwFlagDroppedMessage {
  189.   public:
  190.     InAddr        dropper;        // ship that dropped flag
  191.     TeamInfo        info;            // new team information
  192. };
  193.  
  194. struct SwFlagCapturedMessage {
  195.   public:
  196.     InAddr        captor;            // ship that captured flag
  197.     TeamInfo        flagTeam;        // team whose flag it is
  198.     TeamInfo        captorTeam;        // team who captured flag
  199. };
  200.  
  201. enum WorldType        { WorldNone = 0,
  202.               WorldTeam = 1,
  203.               WorldPlayer = 2 };
  204.  
  205. struct SwWorldMessage {
  206.   public:
  207.     WorldType        type;            // type of info
  208.     int            count;            // number of messages to go
  209.     union {
  210.       PlayerInfo    player;
  211.       TeamInfo        team;
  212.     }            info;
  213. };
  214.  
  215. struct SwKilledMessage {
  216.   public:
  217.     InAddr        killer;            // who killed
  218.     InAddr        victim;            // who got killed
  219. };
  220.  
  221. // if missileNum == -1:
  222. //    position = local direction of ship (normalize)
  223. // else
  224. //    position = position of impact in space
  225. // momentum is in world space (mass already accounted for)
  226. struct SwHitMessage {
  227.   public:
  228.     InAddr        hitter;            // who did hitting
  229.     InAddr        victim;            // who has been hit
  230.     int            missileNum;        // -1 if ship, else missile
  231.     float        position[3];        // position of hit
  232.     float        velocityChange[3];    // momentum imparted
  233.     float        energy;            // energy of hit
  234. };
  235.  
  236. struct SwMessageMessage {
  237.   public:
  238.     InAddr        source;            // who sent it
  239.     InAddr        target;            // who's it about
  240.     Team        team;            // or which team
  241.     char        msg[81];        // null terminated string
  242. };
  243.  
  244. // AnyMessage can hold exactly one of any type of message
  245. union SwAnyMessage {
  246. //  public:
  247.     SwJoinMessage        join;
  248.     SwQuitMessage        quit;
  249.     SwGetWorldMessage        getWorld;
  250.     SwAliveMessage        alive;
  251.     SwGrabFlagMessage        grabFlag;
  252.     SwDropFlagMessage        dropFlag;
  253.     SwCaptureFlagMessage    captureFlag;
  254.     SwAddPlayerMessage        addPlayer;
  255.     SwRemovePlayerMessage    removePlayer;
  256.     SwFlagUpdateMessage        flagUpdate;
  257.     SwFlagGrabbedMessage    flagGrabbed;
  258.     SwFlagDroppedMessage    flagDropped;
  259.     SwFlagCapturedMessage    flagCaptured;
  260.     SwWorldMessage        world;
  261.     SwKilledMessage        killed;
  262.     SwHitMessage        hit;
  263.     SwMessageMessage        message;
  264. };
  265.  
  266. // ServerPacket holds one message plus type and size info for that message
  267. struct ServerPacket {
  268.   public:
  269.     int            length;            // length of message
  270.     ServerMessageType    type;            // message type
  271.     SwAnyMessage    any;            // message
  272. };
  273.  
  274. // openBroadcastSocket() returns socket fd (-1 on failure)
  275. int            openBroadcastSocket(int port, struct sockaddr_in*);
  276.  
  277. #endif
  278.